package demo.codeanalyzer.processor;

import java.util.HashMap;
import java.util.Map;

import com.sun.source.tree.ClassTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.Trees;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation.ANONYMOUS;

public class NameSpaceCodeGenerator extends BaseCodeGenerator implements
		CodeGenerator {

	private Map<String, String> nameSpaces = new HashMap<String, String>();

	public Object printClass(ClassTree classTree, Trees trees) {
		nameSpaces.put("xsd", "http://www.w3.org/2001/XMLSchema#");
		nameSpaces.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
		nameSpaces.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
		Object result = visitor.visitParentClass(classTree, trees);
		printNameSpaces();
		writeOntologyDefinition(trees);
		writeNameSpaceDefinition(trees);
		return result;

	}

	private void writeNameSpaceDefinition(Trees trees) {
		System.out.println();
		String nameSpace = nameSpace(trees);
		if (nameSpace.length() > 0) {
			printStartNameSpaceDefintion(nameSpace);
			printNameSpaceBody(nameSpace);
			printEndNameSpaceDefinition(nameSpace);
		}
	}

	private void printEndNameSpaceDefinition(String nameSpace) {
		System.out.println("</chs:Namespace>");
	}

	private void printNameSpaceBody(String nameSpace) {
		System.out.println(nameSpacePrefixString(nameSpace));
	}

	private String nameSpacePrefixString(String nameSpace) {
		return String.format("<chs:prefix>%s</chs:prefix>", unquote(nameSpace));
	}

	private void printStartNameSpaceDefintion(String nameSpace) {
		System.out.println(mainNameSpaceString(nameSpace));
	}

	private String mainNameSpaceString(String nameSpace) {
		return String.format("<chs:Namespace rdf:about=\"&%s;\">",
				unquote(nameSpace));
	}

	private void writeOntologyDefinition(Trees trees) {
		System.out.println();
		String nameSpace = nameSpace(trees);
		if (canWriteOntologyDefiniton(nameSpace)) {
			startOntologyDefiniton(nameSpace);
			writeOntologyComment(trees);
			writeOntologyLabel(trees);
			writeOntologyImports(trees);
			endOntologyDefinition();
		}
	}

	private String nameSpace(Trees trees) {
		return annotationValue(element(trees), "namespace");
	}

	private void writeOntologyImports(Trees trees) {
		String imports = annotationValue(element(trees), "imports");
		if (imports.length() > 0) {
			System.out.println(importString(unquote(imports)));
		}
	}

	private String importString(String imports) {
		return String.format("<owl:imports rdf:resource=\"%s\"", imports);
	}

	private void writeOntologyLabel(Trees trees) {
		String rdfLabel = annotationValue(element(trees), "rdflabel");
		if (rdfLabel.length() > 0) {
			System.out.println(rdfLableString(unquote(rdfLabel)));
		}
	}

	private String rdfLableString(String rdfLabel) {
		return String.format("<rdfs:label>%s</rdfs:label>", rdfLabel);
	}

	private void writeOntologyComment(Trees trees) {
		String rdfComment = annotationValue(element(trees), "rdfcomment");
		if (rdfComment.length() > 0) {
			System.out.println(rdfCommentString(unquote(rdfComment)));
		}
	}

	private String rdfCommentString(String rdfComment) {
		return String.format("<rdfs:comment>%s</rdfs:comment>", rdfComment);
	}

	private void endOntologyDefinition() {
		System.out.println("</owl:Ontology>");
	}

	private void startOntologyDefiniton(String nameSpace) {
		System.out.println(String.format("<owl:Ontology rdf:about=\"&;%s\"",
				unquote(nameSpace)));
	}

	private String unquote(String nameSpace) {
		return nameSpace.replace("\"", "");
	}

	private boolean canWriteOntologyDefiniton(String nameSpace) {
		return nameSpace.length() > 0;
	}

	private void printNameSpaces() {
		printEntityNameSpaces();
		System.out.println("<rdf:RDF ");
		System.out.println("xmlns:owl=\"http://www.w3.org/2002/07/owl#\"");
		for (String nameSpaceIdentifier : nameSpaces.keySet()) {
			System.out.println(xmlnsString(nameSpaceIdentifier));
		}
		System.out.println(">");
		System.out.println();
	}

	private String xmlnsString(String nameSpaceIdentifier) {
		String nameSpaceUri = nameSpaces.get(nameSpaceIdentifier);
		if (nameSpaceUri.startsWith("\"") && nameSpaceUri.endsWith("\"")) {
			nameSpaceUri = nameSpaceUri.replace("\"", "");
		}
		return String.format("xmlns:%s=\"%s\" ", nameSpaceIdentifier,
				nameSpaceUri);
	}

	private void printEntityNameSpaces() {
		System.out.println("<!DOCTYPE rdf:RDF [");
		for (String nameSpaceIdentifier : nameSpaces.keySet()) {
			System.out.println(namespaceString(nameSpaceIdentifier, nameSpaces
					.get(nameSpaceIdentifier)));
		}
		System.out.println("]>");
		System.out.println();
	}

	private String namespaceString(String nameSpaceIdentifier,
			String nameSpaceUri) {
		if (nameSpaceUri.startsWith("\"") && nameSpaceUri.endsWith("\"")) {
			nameSpaceUri = nameSpaceUri.replace("\"", "");
		}
		return String.format("<!ENTITY %s \"%s\" >", nameSpaceIdentifier,
				nameSpaceUri);
	}

	public Object printEnum(ClassTree classTree, Trees trees) {
		return null;
	}

	public void printMethod(MethodTree methodTree) {
	}

	public Object printNamespaces(ClassTree classTree, Trees trees) {
		return null;
	}

	public Object printVariable(VariableTree variableTree, Trees trees) {
		String nameSpaceValue = variableTree.toString();
		String[] nameSpaceParts = nameSpaceValue.split(" ");
		String nameSpaceIdentifier = null;
		String nameSpaceUri = null;
		for (int i = 0; i < nameSpaceParts.length; i++) {
			if (i == 1) {
				nameSpaceIdentifier = nameSpaceParts[i];
			}
			if (i == 3) {
				nameSpaceUri = nameSpaceParts[i];
			}
		}
		nameSpaces.put(nameSpaceIdentifier, nameSpaceUri);
		return visitor.visitParentVariable(variableTree, trees);
	}

	public Object processNamespaces(VariableTree variableTree, Trees trees) {
		// TODO Auto-generated method stub
		return null;
	}

}
